﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for CarUserContactDB
/// </summary>
namespace CE.CarInsurance
{
    public class CarUserContactDB
    {
        public Int64 Insert(CarUserContact carusercontact)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocUserContactInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ContactId", carusercontact.ContactId);
            _sPara.Add("@Email", carusercontact.Email == string.Empty ? string.Empty : carusercontact.Email);
            _sPara.Add("@Mobile", carusercontact.Mobile == string.Empty ? string.Empty : carusercontact.Mobile);
            _sPara.Add("@HomePostCode", carusercontact.HomePostCode == string.Empty ? string.Empty : carusercontact.HomePostCode);
            _sPara.Add("@OfficePostCode", carusercontact.OfficePostCode == string.Empty ? string.Empty : carusercontact.OfficePostCode);
            _sPara.Add("@HomeUnitNo", carusercontact.HomeUnitNo);
            _sPara.Add("@OfficeUnitNo", carusercontact.OfficeUnitNo);
            _sPara.Add("@HomeLevelNo", carusercontact.HomeLevelNo);
            _sPara.Add("@OfficeLevelNo", carusercontact.OfficeLevelNo);
            _sPara.Add("@HomeStreet", carusercontact.HomeStreet);
            _sPara.Add("@OfficeStreet", carusercontact.OfficeStreet);
            _sPara.Add("@HomeFax", carusercontact.HomeFax);
            _sPara.Add("@OfficeFax", carusercontact.OfficeFax);
            _sPara.Add("@HomePhone", carusercontact.HomePhone == string.Empty ? string.Empty : carusercontact.HomePhone);
            _sPara.Add("@OfficePhone", carusercontact.OfficePhone == string.Empty ? string.Empty : carusercontact.OfficePhone);
            _sPara.Add("@HomeCity", carusercontact.HomeCity);
            _sPara.Add("@HomeCountry", carusercontact.HomeCountry);
            _sPara.Add("@OfficeCity", carusercontact.OfficeCity);
            _sPara.Add("@OfficeCountry", carusercontact.OfficeCountry);
            _sPara.Add("@Status", carusercontact.Status);
            _sPara.Add("@OtherContact1", carusercontact.OtherContact1);
            _sPara.Add("@OtherContact2", carusercontact.OtherContact2);
            _sPara.Add("@RefCEUserId", carusercontact.RefCEUserId);
            _sPara.Add("@Info1", carusercontact.Info1);
            _sPara.Add("@Info2", carusercontact.Info2);
            _sPara.Add("@Info3", carusercontact.Info3);
            _sPara.Add("@Info4", carusercontact.Info4);
            _sPara.Add("@Info5", carusercontact.Info5);
            _sPara.Add("@Info6", carusercontact.Info6);
            _sPara.Add("@Info7", carusercontact.Info7);
            _sPara.Add("@Info8", carusercontact.Info8);
            _sPara.Add("@Info9", carusercontact.Info9);
            _sPara.Add("@Info10", carusercontact.Info10);
            _sPara.Add("@Info11", carusercontact.Info11);
            _sPara.Add("@Info12", carusercontact.Info12);
            _sPara.Add("@Info13", carusercontact.Info13);
            _sPara.Add("@Info14", carusercontact.Info14);
            _sPara.Add("@Info15", carusercontact.Info15);
            _sPara.Add("@Info16", carusercontact.Info16);
            _sPara.Add("@Info17", carusercontact.Info17);
            _sPara.Add("@Info18", carusercontact.Info18);
            _sPara.Add("@Info19", carusercontact.Info19);
            _sPara.Add("@Info20", carusercontact.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarUserContact GetContact(Int64 RefCEUserID)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocUserContactSelectSingleItem";
            CarUserContact _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@RefCEUserId", RefCEUserID);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new CarUserContact();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        private CarUserContact FillDataRecord(DataTable myDataTable)
        {
            CarUserContact myCarUserContact = new CarUserContact();
            myCarUserContact.ContactId = Convert.ToInt64(myDataTable.Rows[0]["ContactId"].ToString());
            myCarUserContact.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());

            string _email = myDataTable.Rows[0]["Email"].ToString();
            if (_email != string.Empty)
            {
                myCarUserContact.Email = _email;
            }

            string _mobile = myDataTable.Rows[0]["Mobile"].ToString();
            if (_mobile != string.Empty)
            {
                myCarUserContact.Mobile = _mobile;
            }

            string _homephone = myDataTable.Rows[0]["HomePhone"].ToString();
            if (_homephone != string.Empty)
            {
                myCarUserContact.HomePhone = _homephone;
            }

            myCarUserContact.HomeFax = myDataTable.Rows[0]["HomeFax"].ToString();
            myCarUserContact.HomeUnitNo = myDataTable.Rows[0]["HomeUnitNo"].ToString();
            myCarUserContact.HomeLevelNo = myDataTable.Rows[0]["HomeLevelNo"].ToString();
            myCarUserContact.HomeStreet = myDataTable.Rows[0]["HomeStreet"].ToString();
            myCarUserContact.HomeCity = myDataTable.Rows[0]["HomeCity"].ToString();
            myCarUserContact.HomeCountry = myDataTable.Rows[0]["HomeCountry"].ToString();

            string _homepostcode = myDataTable.Rows[0]["HomePostCode"].ToString();
            if (_homepostcode != string.Empty)
            {
                myCarUserContact.HomePostCode = _homepostcode;
            }

            myCarUserContact.OfficeUnitNo = myDataTable.Rows[0]["OfficeUnitNo"].ToString();
            myCarUserContact.OfficeUnitNo = myDataTable.Rows[0]["OfficeLevelNo"].ToString();

            string _officephone = myDataTable.Rows[0]["OfficePhone"].ToString();
            if (_officephone != string.Empty)
            {
                myCarUserContact.OfficePhone = _officephone;
            }

            myCarUserContact.OfficeFax = myDataTable.Rows[0]["OfficeFax"].ToString();
            myCarUserContact.OfficeStreet = myDataTable.Rows[0]["OfficeStreet"].ToString();
            myCarUserContact.OfficeCity = myDataTable.Rows[0]["OfficeCity"].ToString();
            myCarUserContact.OfficeCountry = myDataTable.Rows[0]["OfficeCountry"].ToString();

            string _officepostcode = myDataTable.Rows[0]["OfficePostCode"].ToString();
            if (_officepostcode != string.Empty)
            {
                myCarUserContact.OfficePostCode = _officepostcode;
            }

            myCarUserContact.Info1 = myDataTable.Rows[0]["Info1"].ToString();
            myCarUserContact.Info2 = myDataTable.Rows[0]["Info2"].ToString();
            myCarUserContact.Info3 = myDataTable.Rows[0]["Info3"].ToString();
            myCarUserContact.Info4 = myDataTable.Rows[0]["Info4"].ToString();
            myCarUserContact.OtherContact1 = myDataTable.Rows[0]["OtherContact1"].ToString();
            myCarUserContact.OtherContact2 = myDataTable.Rows[0]["OtherContact2"].ToString();
            return myCarUserContact;
        }

    }
}